package com.example.marcus.tokentest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity {

    private static final String OTHER1 = "You choose the other 1";
    private static final String OTHER2 = "you choose the other 2";
    private ViewPager vp;
    private List<TabFragment> fragmentList = new ArrayList<TabFragment>();
    private String[] mTitle = new String[]{
            "tab 1",
            "tab 2",
            "tab 3",
            "tab 4"};
    private FragmentPagerAdapter fpg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setOverFlowButtonShow();
        getActionBar().setDisplayShowHomeEnabled(false);

        //init views
        initView();
        // init datas
        initDatas();

        vp.setAdapter(this.fpg);
    }

    private void initDatas() {
        for (String tmptitle : this.mTitle) {
            TabFragment tmp = new TabFragment();
            Bundle bundle = new Bundle();
            bundle.putString(TabFragment.TITLE, tmptitle);
            tmp.setArguments(bundle);
            fragmentList.add(tmp);
        }

        this.fpg = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
    }

    private void initView() {
        vp = (ViewPager) findViewById(R.id.id_view_pager);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        String message = "";
        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_add_token:
                message = getResources().getString(R.string.add_message);
                break;
            case R.id.action_other_1:
                message = MainActivity.OTHER1;
                break;
            case R.id.action_other_2:
                message = MainActivity.OTHER2;
                break;
            case R.id.action_settings:
                message = "you chose the settings";
                break;
            default:
                message = "";
                break;
        }


        Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
        toast.show();

        return super.onOptionsItemSelected(item);
    }


    /**
     * Set OverFlowButton Style
     */
    private void setOverFlowButtonShow() {
        ViewConfiguration vc = ViewConfiguration.get(this);
        try {
            Field overFlowButtonField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            overFlowButtonField.setAccessible(true);
            overFlowButtonField.setBoolean(vc, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
